<!-- 车型管理 -->
<template>
  <el-card class="Top1">

    <div class="dashboard-container car-models customer-list-box">
      <div class="top">
        <el-form
          ref="serachBuildingFrom"
          :model="carModelDataParams"
        >
          <el-row
            type="flex"
            justify="space-between"
          >
            <el-form-item
              label-width="10vh"
              label="车型编号:"
              prop="id"
            >
              <el-input
                v-model="carModelDataParams.id"
                placeholder="请输入车型编号"
                style="width: 20vw"
              />
            </el-form-item>
            <el-form-item
              label-width="10vh"
              label="应载重量:"
              prop="allowableLoad"
            >
              <el-select
                v-model="carModelDataParams.allowableLoad"
                placeholder="请选择应载重量"
                style="width: 20vw"
              >
                <el-option
                  v-for="(item, index) in allowableLoadList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label-width="10vh"
              label="应载体积:"
              prop="allowableVolume"
            >
              <el-select
                v-model="carModelDataParams.allowableVolume"
                placeholder="请选择应载体积"
                style="width: 20vw"
              >
                <el-option
                  v-for="(item, index) in allowableVolumeList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-row>
          <el-row
            type="flex"
            justify="right"
          >
            <el-form-item
              label-width="10vh"
              label="车辆类型:"
              style="margin-right: 2vw;"
              prop="name"
            >
              <el-input
                v-model="carModelDataParams.name"
                placeholder="请输入车辆类型"
                style="width: 20vw"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="warning"
                style="margin-left: 20px"
                @click="seek"
              >搜索</el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                style="margin-left: 20px"
                @click="reset"
              >重置</el-button>
            </el-form-item>
          </el-row>
          <!-- 查询 -->
        </el-form>
      </div>
      <!-- 新增按钮 -->
      <div class="bottom">
        <el-button
          type="warning"
          style="margin: 20px"
          size="small"
          @click="centerDialogVisible = true"
        >新增车型</el-button>
        <!-- tanle表单 -->
        <el-table
          style="width: 100%"
          :data="commonList"
        >
          <el-table-column
            type="index"
            label="序号"
          />columnparams
          <el-table-column
            prop="id"
            label="车型编号"
          />
          <el-table-column
            prop="name"
            label="车辆类型"
          />
          <el-table-column
            prop="num"
            label="车型数量"
          />
          <el-table-column
            prop="allowableLoad"
            label="应载重量（吨）"
          />
          <el-table-column
            prop="allowableVolume"
            label="应载体积（立方米）"
          />
          <el-table-column
            prop="measureLong"
            label="长（米）"
          />
          <el-table-column
            prop="measureWidth"
            label="宽（米）"
          />
          <el-table-column
            prop="measureHigh"
            label="高（米）"
          />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="text"
                size="small"
                @click="edit(row)"
              >编辑</el-button>
              <el-divider direction="vertical" />
              <el-button
                :disabled="row.status === 1"
                type="text"
                size="small"
                @click="del(row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row
          type="flex"
          justify="center"
          style="margin-top: 20px"
        >
          <el-pagination
            :current-page.sync="carModelDataParams.page"
            :page-sizes="[10, 20, 50, 100]"
            layout="total,  prev, pager, sizes,next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="carLIsts"
          />
        </el-row>
        <!-- 弹框 -->
        <div class="form-container">
          <el-dialog
            title="新增车型"
            :visible.sync="centerDialogVisible"
            width="40%"
            center
          >
            <el-form
              ref="form"
              :label-position="labelPosition"
              label-width="80px"
              :model="ruleForm"
              :rules="rules"
            >
              <el-form-item
                label="车辆类型"
                prop="name"
              >
                <el-input
                  v-model="ruleForm.name"
                  placeholder="请输入车辆类型"
                  style="width: 30vw"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="应载重量"
                prop="allowableLoad"
              >
                <el-input
                  v-model="ruleForm.allowableLoad"
                  placeholder="请输入应载重量"
                  style="width: 30vw"
                >
                  <template slot="suffix">吨</template>
                </el-input>
              </el-form-item>
              <el-form-item
                label="应载体积"
                prop="allowableVolume"
              >
                <el-input
                  v-model="ruleForm.allowableVolume"
                  placeholder="请输入应载体积"
                  style="width: 30vw"
                >
                  <template slot="suffix">立方</template>
                </el-input>
              </el-form-item>
              <el-form-item label="长">
                <el-input
                  v-model="ruleForm.measureLong"
                  style="width: 30vw"
                >
                  <template slot="suffix">米</template>
                </el-input>
              </el-form-item>
              <el-form-item label="宽">
                <el-input
                  v-model="ruleForm.measureWidth"
                  style="width: 30vw"
                >
                  <template slot="suffix">米</template>
                </el-input>
              </el-form-item>
              <el-form-item label="高">
                <el-input
                  v-model="ruleForm.measureHigh"
                  style="width: 30vw"
                >
                  <template slot="suffix">米</template>
                </el-input>
              </el-form-item>
            </el-form>
            <span
              slot="footer"
              class="dialog-footer"
            >
              <el-button
                type="danger"
                @click="add"
              >确 定</el-button>
              <el-button @click="centerDialogVisible = false">取 消</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </el-card>
</template>
<script>
import { list, del, add, update } from '@/api/transit'
export default {
  data() {
    return {
      labelPosition: 'right',
      centerDialogVisible: false,
      // 获取车辆列表的重量数组
      allowableLoadList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      // 获取车辆列表的体积数组
      allowableVolumeList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      ruleForm: {
        allowableLoad: '', // 准载重量
        allowableVolume: '', // 准载体积
        goodsTypes: null,
        id: '',
        measureHigh: '0.0', // 高
        measureLong: '0.0', // 长
        measureWidth: '0.0', // 宽
        name: '', // 车辆类型名称
        num: null
      },
      // 规则
      rules: {
        allowableLoad: [
          { required: true, message: '请输入准载重量', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '请输入准载体积', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入车辆类型', trigger: 'blur' }
        ]
      },
      carModelDataParams: {
        name: '', // 车辆类型名称
        allowableLoad: '', // 车型载重
        allowableVolume: '', // 车型体积
        id: '', // 车型编号
        page: 1, // 当前页
        pageSize: 10 // 每页大小
      },
      // 总条数
      total: 0,
      // 车辆类型信息
      commonList: []
    }
  },
  created() {
    this.carLIsts()
  },
  methods: {
    async carLIsts() {
      const res = await list(this.carModelDataParams)
      console.log('车型管理渲染数据', res)
      this.commonList = res.data.items
      this.total = parseInt(res.data.counts)
      console.log(this.total)
    },
    sizeChange(e) {
      // 把选中的页数赋值给查询参数
      this.carModelDataParams.pageSize = e
      // 重新发送请求
      this.carLIsts()
    },
    // 搜索
    seek() {
      this.carLIsts()
    },
    // 重置表单
    reset() {
      this.$refs.serachBuildingFrom.resetFields()
      this.carLIsts()
    },

    // 删除功能
    del(row) {
      this.$confirm('您确认要删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await del(row.id)
        this.$message.success('删除成功')
        this.carLIsts()
      }).catch(() => {
        // 点击取消
      })
    },
    // 新增
    add() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.ruleForm.id) {
            const id = this.ruleForm.id
            delete this.ruleForm.id
            await update(id, this.ruleForm)
            this.centerDialogVisible = false
            this.$refs.form.resetFields()
            this.carLIsts()
          } else {
            await add(this.ruleForm)
            this.centerDialogVisible = false
            this.$refs.form.resetFields()
            this.carLIsts()
          }
        }
      })
    },
    // 回显
    edit(row) {
      console.log('编辑', row.id)
      console.log('编辑', row)
      this.centerDialogVisible = true
      this.ruleForm.allowableLoad = row.allowableLoad
      this.ruleForm.allowableVolume = row.allowableVolume
      this.ruleForm.name = row.name
      this.ruleForm.id = row.id
      this.ruleForm.measureHigh = row.measureHigh
      this.ruleForm.measureLong = row.measureLong
      this.ruleForm.measureWidth = row.measureWidth
    }

  }

}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.form-container {
  padding: 0px 80px;
}

.Top1 {
  padding-bottom: 40px;
}

.add-btn {
  margin-bottom: 20px;
}

.alert {
  margin: 10px 0px;
}

.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.top {
  padding-bottom: -60px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.car-models {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }

  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }

  .warn-img {
    width: 40px;
  }

  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }

}
</style>
